package com.zlzxm.studentscreen.ui.ijkplayer;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;

import com.zlzxm.studentscreen.R;


/**
 * CREATE BY zlzxm ON 2022/3/1
 */
public class RtspView extends RelativeLayout implements SurfaceHolder.Callback,
        PlayerEventListener {

    private static String TAG = "RtspView";

    private static final int ID_BTN1 = 1;
    private static final int ID_BTN2 = 2;
    private static final int ID_BTN3 = 3;
    private static final int ID_BTN4 = 4;


    private int mWidth;
    private int mHeight;

    IjkPlayer ijkPlayer;
    SurfaceView surfaceView;
    private String url;
    private RelativeLayout rlEmpty;


    public void release(){

        if(ijkPlayer!=null){
            Log.e("zlz","rtspPlayer release");
            ijkPlayer.reset();
            ijkPlayer.release();
            ijkPlayer = null;
        }


    }


    public RtspView(Context context) {
        this(context, null);
    }

    public RtspView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RtspView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        mHeight = h;
    }

    private void init(){


        createSurfaceView();

        rlEmpty = (RelativeLayout) LayoutInflater.from(getContext()).inflate(R.layout.layout_camera_empty,null);

        LayoutParams lpEmpty = new LayoutParams(-1,-1);

        addView(rlEmpty,lpEmpty);

    }

    public void setUrl(String url) {
        this.url = url;
    }

    private void createSurfaceView() {
        //生成一个新的surface view
        surfaceView = new SurfaceView(getContext());
        surfaceView.getHolder().addCallback(this);
        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT
                , LayoutParams.MATCH_PARENT);
        surfaceView.setId(ID_BTN1);
        layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
        surfaceView.setLayoutParams(layoutParams);
        this.addView(surfaceView);

        View leftLine = new View(getContext());
        leftLine.setBackgroundColor(Color.RED);

        LayoutParams lpLL = new LayoutParams(10
                , LayoutParams.MATCH_PARENT);
        lpLL.addRule(RelativeLayout.ALIGN_LEFT,ID_BTN1);
        leftLine.setLayoutParams(lpLL);
        this.addView(leftLine);


        View rightLine = new View(getContext());
        rightLine.setBackgroundColor(Color.GREEN);

        LayoutParams lpRl = new LayoutParams(10
                , LayoutParams.MATCH_PARENT);
        lpRl.addRule(RelativeLayout.ALIGN_RIGHT,ID_BTN1);
        rightLine.setLayoutParams(lpRl);
        this.addView(rightLine);


        View topLine = new View(getContext());
        topLine.setBackgroundColor(Color.YELLOW);

        LayoutParams lpTl = new LayoutParams( LayoutParams.MATCH_PARENT
                , 10);
        lpTl.addRule(RelativeLayout.ALIGN_TOP,ID_BTN1);
        topLine.setLayoutParams(lpTl);
        this.addView(topLine);


        View bottomLine = new View(getContext());
        bottomLine.setBackgroundColor(Color.BLUE);

        LayoutParams lpBl = new LayoutParams( LayoutParams.MATCH_PARENT
                ,  10);
        lpBl.addRule(RelativeLayout.ALIGN_BOTTOM,ID_BTN1);
        bottomLine.setLayoutParams(lpBl);
        this.addView(bottomLine);

    }


    private void createPlayer() {
        if (ijkPlayer != null) {
            ijkPlayer.stop();
            ijkPlayer.setDisplay(null);
            ijkPlayer.release();
        }
        ijkPlayer = new IjkPlayer(getContext());
        ijkPlayer.initPlayer();
//        ijkMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "mediacodec", 1);
        ijkPlayer.setDisplay(surfaceView.getHolder());
        ijkPlayer.bindVideoView(this);
    }



    public void start(String url) {
        //每次都要重新创建IMediaPlayer
        Log.e(TAG,"createPlayer");
        this.url = url;
        createPlayer();
        ijkPlayer.setDisplay(surfaceView.getHolder());
        ijkPlayer.setDataSource(url, null);
        ijkPlayer.prepareAsync();


    }

    @Override
    public void surfaceCreated(@NonNull SurfaceHolder holder) {
        Log.e(TAG,"surfaceCreated");
        if(ijkPlayer!=null) {
            ijkPlayer.setDisplay(surfaceView.getHolder());
        }

    }

    @Override
    public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {

        Log.e("zlz","surfaceChanged==========》");
        Log.e("zlz","format " + format + "width:"+width +  "  height ： "+height);

    }

    @Override
    public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
        Log.e("zlz","surfaceDestroyed");
    }

    @Override
    public void onError() {
        Log.e(TAG,"onError");
    }

    @Override
    public void onCompletion() {

        Log.e(TAG,"onCompletion====》"  );
    }

    @Override
    public void onInfo(int what, int extra) {
        Log.e(TAG,"onInfo"+ "WHAT :" + what + "extra:"+extra+"====>");
        if(what == 10001){
            rlEmpty.setVisibility(GONE);
        }
    }

    @Override
    public void onPrepared() {
        ijkPlayer.start();
    }

    @Override
    public void onVideoSizeChanged(int width, int height) {
        Log.e("zlz","onVideoSizeChanged : "+"width:"+width + "  height:"+height);
        double wRate = mWidth * 1.0/ width;
        double hRate = mHeight * 1.0 / height;
        double rate = Math.min(wRate,hRate);

        LayoutParams layoutParams = (LayoutParams) surfaceView.getLayoutParams();
        layoutParams.width = (int) (width * rate);
        layoutParams.height = (int) (height * rate);
        surfaceView.setLayoutParams(layoutParams);

    }


}
